﻿using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine.SceneManagement;

/// <summary>
/// 系统事件接收，处理器.
/// 使用方法: 需在项目启动Loading Scene上添加空的GameObject,并添加GlobalManager脚本组件，以完成必须的初始化工作。
/// </summary>
public class GlobalManager : MonoBehaviour {
	private static GlobalManager m_instance;
	public static GlobalManager Instance{
		get{
			if(m_instance==null){
				GameObject go = new GameObject("[GlobalManager]");
				return go.AddComponent<GlobalManager>();
			}
			return m_instance;
		}
	}

	public int frameRate = 60;
	public bool multiTouchEnabled = false;
	public UnityEngine.Events.UnityEvent onAppResume;
	public UnityEngine.Events.UnityEvent onInterstitialWillShow;

	void Awake(){
		if (m_instance)
		{
			Destroy(this.gameObject);
			return;
		}
		name = "[GlobalManager]";
		m_instance = this;
		DontDestroyOnLoad(gameObject);
		Application.targetFrameRate = frameRate;
		Input.multiTouchEnabled = multiTouchEnabled;
  	}


	/// <summary>
	/// 回到应用(弹全屏广告或者视频广告，不应该触发此回调)
	/// </summary>
	private void OnApplicationWillEnterForeground(){
		if(onAppResume!=null) onAppResume.Invoke();
		if(!PluginManager.Instance.IsAdRemovedByIAP){
			if (SceneManager.GetActiveScene ().buildIndex > PluginManager.Instance.showBannerSceneIdx-1 && !PluginManager.Instance.isIap && !PluginManager.Instance.CheckScreenAdIsShowed ()) {
				Debug.Log ("OnApplicationWillEnterForeground: will show interstitial.");
				if(PluginManager.Instance.adsPlugin.ShowAd((int)AdType.AdTypeInterstitialAds)){
					if(onInterstitialWillShow!=null) onInterstitialWillShow.Invoke();
				}
			}
		}
	}

}
